#include "changeinfo.h"
#include "ui_changeinfo.h"
#include<QSqlQuery>
#include<QMessageBox>
#include<QSqlRecord>
#include<teacher.h>
#include"formt.h"
#include"teacherinfo.h"

changeinfo::changeinfo(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::changeinfo)
{
    ui->setupUi(this);
}

changeinfo::~changeinfo()
{
    delete ui;
}

 void changeinfo::showchangeinfo()
 {
     emit signalchangeinfo();
     this->show();
 }

void changeinfo::on_pb_ok1_clicked()
{
    FormT formt;
    TeacherInfo tea_info = formt.tinfo;
    QString id = formt.id;
    QString sex = ui->lineEdit_Sex->text();
    QString name = ui->lineEdit_Name->text();
    QString birth = ui->lineEdit_Birth->text();
    QSqlQuery query;
    QString queryString =
            QString("update TeacherInfoTable set Sex="+sex +", Name = "+
                    name+",Birth = "+birth+"where ID = '%1'").arg(id);
    if(query.exec(queryString))
    {
        int id_id = query.record().indexOf("ID");
        int TeacherId_id = query.record().indexOf("TeacherID");
        int XID_id = query.record().indexOf("XID");
        int Name_id = query.record().indexOf("Name");
        int Sex_id = query.record().indexOf("Sex");
        int Birth_id = query.record().indexOf("Birth");
        int pswd_id = query.record().indexOf("loginpw");
        while(query.next())
        {
            QString id = query.value(id_id).toString();
            QString Name = query.value(Name_id).toString();
            QString XID = query.value(XID_id).toString();
            QString pswd = query.value(pswd_id).toString();
            QString TeacherID = query.value(TeacherId_id).toString();
            QString Sex = query.value(Sex_id).toString();
            QString Birth = query.value(Birth_id).toString();
             tea_info.updateTeacherInfo(id, TeacherID,XID, Name,Sex, Birth, pswd);
             tea_info.display();
          }
        }else{
            QMessageBox msgBox;
            msgBox.setText("Failed:updata people !!");
            msgBox.setInformativeText("Please try again !");
            msgBox.setStandardButtons(QMessageBox::Retry | QMessageBox::Cancel);
            msgBox.setDefaultButton(QMessageBox::Retry);

            int ret = msgBox.exec();
            switch (ret) {
            case QMessageBox::Retry:
              ui->lineEdit_Sex->setFocus();
              break;
            case QMessageBox::Cancel:
              this->close();
              break;
            default:
              // should never be reached
              break;
            }
          }
}

void changeinfo::showTW()
{
    Teacher *tw = new Teacher;
    tw->show();
    tw->selectBoxSlot(str);
}

void changeinfo::on_pb_cancel1_clicked()
{
    this->close();
    this->showTW();
}
